﻿
using SmartWMS.Common.ResponseOutput;
using SmartWMS.Domain.BasicsDatum;
using SmartWMS.Domain.StatementManage;
using SmartWMS.Server.BusinessServer.AccountingServer.Dto;
using SmartWMS.Server.Repository;
using SmartWMS.Server.StatementServer.InventoryBookServer.Dto;
using SmartWMS.Server.WarehouseManage.InventoryLedgerServer.Dto;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartWMS.Server.StatementServer.InventoryBookServer
{
    /// <summary>
    /// 库存台账
    /// </summary>
    public class InventoryBookServers:IInventoryBookServers
    {
        private readonly IRepository<InventoryBook> _inventoryBookRepository;  // 库存台账
        private readonly IRepository<Product> _products;   // 产品
        private readonly IRepository<Warehouse> _warehouses;    // 仓库

        public InventoryBookServers(IRepository<InventoryBook> inventoryBookRepository, IRepository<Product> products, IRepository<Warehouse> warehouses)
        {
            _inventoryBookRepository = inventoryBookRepository;
            _products = products;
            _warehouses = warehouses;
        }

        /// <summary>
        /// 库存台账列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<Response<List<InventoryBookDtoPes>>> GetInventoryBookDtoPes(InventoryBookDtoPage dto) {
            try
            {
                // 获取库存台账列表
                var invenlist = await _inventoryBookRepository.GetAllListAsync();
                // 获取产品列表
                var productlist = await _products.GetAllListAsync();
                // 获取仓库列表
                var warehouselist = await _warehouses.GetAllListAsync();

                var list = (from a in invenlist
                            join b in productlist on a.ProductCode equals b.ProductCode
                            join c in warehouselist on a.WarehouseId equals c.Id
                            select new InventoryBookDtoPes
                            {
                                Id = a.Id,
                                ProductCode = a.ProductCode,
                                ProductName = b.ProductName,
                                Specs = b.Specs,
                                ProductBatch = a.ProductBatch,
                                Type = a.Type,
                                WarehouseId = a.WarehouseId,
                                Name = c.Name,
                                Number = a.Number,
                                Unit = a.Unit,
                                OriginalStorage = a.OriginalStorage,
                                TargetWarehouse=a.TargetWarehouse,
                                TargetLocation=a.TargetLocation,
                                InventoryBookDate = a.InventoryBookDate
                            }).ToList();
                if (!string.IsNullOrEmpty(dto.ProductCode)) { 
                    list=list.Where(x => x.ProductCode == dto.ProductCode).ToList();
                }
                if (!string.IsNullOrEmpty(dto.ProductName))
                {
                    list = list.Where(x => x.ProductName.Contains(dto.ProductName)).ToList();
                }
                if (dto.WarehouseId > 0) {
                    list = list.Where(x => x.WarehouseId == dto.WarehouseId).ToList();
                }
                if (!string.IsNullOrEmpty(dto.ProductBatch))
                {
                    list = list.Where(x => x.ProductBatch == dto.ProductBatch).ToList();
                }
                int totalCount=list.Count();
                int pageCount = (int)Math.Ceiling(totalCount * 1.0 / dto.Size);
                list = list.Skip((dto.Page - 1) * dto.Size).Take(dto.Size).ToList();

                return new Response<List<InventoryBookDtoPes>> { Data = list, TotalCount = totalCount, PageCount = pageCount };

            }
            catch (Exception)
            {

                throw;
            }
        }


    }
}
